package cn.itsource.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.CipherMode;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.domain.VipUser;
import cn.itsource.dto.CodeDto;
import cn.itsource.dto.RegDto;
import cn.itsource.dto.SmsDto;
import cn.itsource.exception.MyException;
import cn.itsource.mapper.VipUserMapper;
import cn.itsource.service.IVipUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author fengluochen
 * @since 2022-11-18
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private VipUserMapper vipUserMapper;
    @Override
    public void register(RegDto dto) {
        //判断密码
        if(!StringUtils.hasLength(dto.getPassword())){
            throw new MyException("请输入密码");
        }
        //判断验证码
        String code = (String) redisTemplate.opsForValue().get(dto.getMobile());
        if(!code.equals(dto.getSmsCode())){
            throw  new  MyException("验证码错误");
        }
        //开始入库
        VipUser vipUser = new VipUser();
        //设置盐值
        String salt = IdUtil.fastSimpleUUID();
        vipUser.setSalt(salt);
        String pwd = MD5.create().setSalt(salt.getBytes()).digestHex(dto.getPassword());
        vipUser.setPassword(pwd);
        vipUser.setPhone(dto.getMobile());
        vipUserMapper.insert(vipUser);


    }
}
